package cordova.plugin.bugly;

import org.apache.cordova.BuildConfig;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;

import android.util.Log;
import android.webkit.WebView;
import org.json.JSONException;
import org.json.JSONObject;

import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.bugly.crashreport.CrashReport.UserStrategy;

import java.io.File;
import java.util.Scanner;

public class CDVBugly extends CordovaPlugin {
    public static final String TAG = "Cordova.Plugin.Bugly";
    private String APP_ID;
    private static final String BUGLY_APP_ID = "ANDROID_APPID";
    public static final String ERROR_INVALID_PARAMETERS = "参数格式错误";

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        APP_ID = webView.getPreferences().getString(BUGLY_APP_ID, "");
    }

    @Override
    public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {

        if (action.equals("enableJSMonitor")) {
            return this.enableJSMonitor(args, callbackContext);
        } else if (action.equals("setTagID")) {
            return this.setTagID(args, callbackContext);
        } else if (action.equals("setUserID")) {
            return this.setUserID(args, callbackContext);
        } else if (action.equals("putUserData")) {
            return this.putUserData(args, callbackContext);
        } else if (action.equals("testJavaCrash")) {
            return this.testJavaCrash(args, callbackContext);
        } else if (action.equals("testNativeCrash")) {
            return this.testNativeCrash(args, callbackContext);
        } else if (action.equals("testANRCrash")) {
            return this.testANRCrash(args, callbackContext);
        }

        return false;
    }

    private boolean enableJSMonitor(CordovaArgs args, CallbackContext callbackContext) {
        this.cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CrashReport.setJavascriptMonitor((WebView)webView.getView(), true);
            }
        });
        callbackContext.success();
        return true;
    }

    private boolean setTagID(CordovaArgs args, CallbackContext callbackContext) {
        try {
            int id = args.getInt(0);
            CrashReport.setUserSceneTag(this.cordova.getActivity().getApplicationContext(), id);
        } catch (JSONException e) {
            callbackContext.error(ERROR_INVALID_PARAMETERS);
            return true;
        }
        callbackContext.success();
        return true;
    }

    private boolean setUserID(CordovaArgs args, CallbackContext callbackContext) {
        try {
//            int id = args.getInt(0);
            String id = args.getString(0);
            CrashReport.setUserId(this.cordova.getActivity().getApplicationContext(), id);
        } catch (JSONException e) {
            callbackContext.error(ERROR_INVALID_PARAMETERS);
            return true;
        }
        callbackContext.success();
        return true;
    }

    private boolean putUserData(CordovaArgs args, CallbackContext callbackContext) {
        try {
            String key = args.getString(0);
            String value = args.getString(1);
            CrashReport.putUserData(this.cordova.getActivity().getApplicationContext(), key, value);
        } catch (JSONException e) {
            callbackContext.error(ERROR_INVALID_PARAMETERS);
            return true;
        }
        callbackContext.success();
        return true;
    }

    private boolean testJavaCrash(CordovaArgs args, CallbackContext callbackContext) {
        this.cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CrashReport.testJavaCrash();
            }
        });
        callbackContext.success();
        return true;
    }

    private boolean testNativeCrash(CordovaArgs args, CallbackContext callbackContext) {
        this.cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CrashReport.testNativeCrash();
            }
        });
        callbackContext.success();
        return true;
    }

    private boolean testANRCrash(CordovaArgs args, CallbackContext callbackContext) {

        this.cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CrashReport.testANRCrash();
            }
        });
        callbackContext.success();
        return true;
    }

}
